$(document).ready(function () {
	var pageTag = $("[name='pageTag']").val();
    $(".nav-item").removeClass("active");
    $("#" + pageTag).addClass("active");

    fetchBorrowedBookList(1, getMiniPageSize());
	fetchBorrowingBookList(1, getMiniPageSize());
    fetchLendedBookList(1, getMiniPageSize());
    fetchLendingBookList(1, getMiniPageSize());

	$(document)
        .on('click', '.thumbnail-img', function(e) {
        	e.preventDefault();
            //alert($(this).attr("value"));
            window.open("Book.aspx?BID="+$(this).attr("value"));
        })
        .on('click', '.borrowing-book-list-pagination-btn', function() {
            fetchBorrowingBookList($(this).attr("value"), getMiniPageSize());
        })
        .on('click', '.borrowed-book-list-pagination-btn', function() {
            fetchBorrowedBookList($(this).attr("value"), getMiniPageSize());
        })
        .on('click', '.lending-book-list-pagination-btn', function() {
            fetchLendingBookList($(this).attr("value"), getMiniPageSize());
        })
        .on('click', '.lended-book-list-pagination-btn', function() {
            fetchLendedBookList($(this).attr("value"), getMiniPageSize());
        })
});
//借入待确认
function fetchBorrowingBookList(pageIndex, pageSize) {
    $.ajax({
        type: "GET",
        data: {PageIndex: pageIndex, PageSize: pageSize, ProcedureID: 5},
        url: "../Handle/BorrowHandlerService.ashx?method=Get_Family_Borrow_Book_List",
        dataType: "json",
        success: function (data) {
            //console.log(data.rows);
            $('#borrowing-book-list').html(getBookListOutputHtml(data.rows));
            $("#borrowing-book-list-pagination").html(getPaginationBtnOutputHtml(data.PageCount, pageIndex, "borrowing-book-list-pagination-btn"));
        }
    });
}
//借入已确认
function fetchBorrowedBookList(pageIndex, pageSize) {
	$.ajax({
        type: "GET",
        data: {PageIndex: pageIndex, PageSize: pageSize, ProcedureID: 6},
        url: "../Handle/BorrowHandlerService.ashx?method=Get_Family_Borrow_Book_List",
        dataType: "json",
        success: function (data) {
            //console.log(data.rows);
            $('#borrowed-book-list').html(getBookListOutputHtml(data.rows));
            $("#borrowed-book-list-pagination").html(getPaginationBtnOutputHtml(data.PageCount, pageIndex, "borrowed-book-list-pagination-btn"));
        }
    });
}
//借出待确认
function fetchLendingBookList(pageIndex, pageSize) {
    $.ajax({
        type: "GET",
        data: {PageIndex: pageIndex, PageSize: pageSize, ProcedureID: 7},
        url: "../Handle/BorrowHandlerService.ashx?method=Get_Family_Borrow_Book_List",
        dataType: "json",
        success: function (data) {
            //console.log(data.rows);
            $('#lending-book-list').html(getBookListOutputHtml(data.rows));
            $("#lending-book-list-pagination").html(getPaginationBtnOutputHtml(data.PageCount, pageIndex, "lending-book-list-pagination-btn"));
        }
    });
}
//借出已确认
function fetchLendedBookList(pageIndex, pageSize) {
    $.ajax({
        type: "GET",
        data: {PageIndex: pageIndex, PageSize: pageSize, ProcedureID: 8},
        url: "../Handle/BorrowHandlerService.ashx?method=Get_Family_Borrow_Book_List",
        dataType: "json",
        success: function (data) {
            //console.log(data.rows);
            $('#lended-book-list').html(getBookListOutputHtml(data.rows));
            $("#lended-book-list-pagination").html(getPaginationBtnOutputHtml(data.PageCount, pageIndex, "lended-book-list-pagination-btn"));
            
        }
    });
}
//
function getBookListOutputHtml(items) {
    var outputHtml = "";
    
    for (var i = 0; i < items.length; i++) {
        var item = items[i];

        var dataContent = "<p><small>" + item["author"] + " / " + item["pym"] + " / " + item["press"] + "</small></p><small>" + subStrToLimitLength(item["Descript"], 150) + "</small>";
        
        outputHtml += "<div class='col-md-4 col-xs-6'>" +
        "<div class='thumbnail' style='border: 0 none;'>" +
            "<img src='" + item["pname"] +
            "' style='width: 110px;height: 160px;' alt='图书封面' data-toggle='popover' title='" + item["bname"] +
            "' data-content='" + dataContent + "' class='thumbnail-img' value='"+item["bid"]+"'>" +
            "<div class='caption'>" +
            "<p>" + subStrToLimitLength(item["bname"], 7) + "</p>" +
            "<p><small>" + subStrToLimitLength(item["author"], 12) + "</small></p>" +
            "</div>" +
        "</div>" +
        "</div>";
    }
    return outputHtml;
}

function getPaginationBtnOutputHtml(btnNum, defaultActiveIndex, btnClass) {
    if (1 == btnNum) {
        return "";
    }

    var outputHtml = "";

    for (var i = 1; i <= btnNum; i++) {
        var active = (defaultActiveIndex == i)?"active":"";
        outputHtml += "<li class='"+active+"'><a href='#' class='"+btnClass+"' value='"+i+"'>"+i+"</a></li>"
    }
    return outputHtml;
}

function getMiniPageSize() {
    return 3;
}